package com.mapreduce.salarytotal;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

//reduce阶段
public class SalaryTotalReducer  extends Reducer<IntWritable,IntWritable,
        IntWritable,IntWritable> {

    private  IntWritable v4 = new IntWritable();
    @Override
    protected void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
         // 1. k4 = k3
        IntWritable k4 = key;//这个key就是reduce输入的key值
        //2. v4 = v3元素的和：遍历v3求和
        int total = 0;
        for (IntWritable value : values) {
            total += value.get();
        }
        //IntWritable v4 = new IntWritable();
        v4.set(total);
        //3.输出k4, v4：context.write()方法
        context.write(k4,v4);
    }
}
